package com.example.hp.storeapp.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.hp.storeapp.Const;
import com.example.hp.storeapp.R;
import com.example.hp.storeapp.base.BaseFragment;
import com.example.hp.storeapp.bean.ChangeMeResult;
import com.example.hp.storeapp.bean.MeResult;
import com.example.hp.storeapp.interf.RetrofitService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends BaseFragment {
    private EditText userNameView;
    private  EditText passwordView;
    private  EditText  phoneView;
    private   EditText  addressView;
    private Button changeTrue;


    public MyFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        userNameView = (EditText) view.findViewById(R.id.change_name);
        passwordView = (EditText) view.findViewById(R.id.change_password);
        phoneView = (EditText) view.findViewById(R.id.change_phone);
        changeTrue = (Button) view.findViewById(R.id.change_true);
        addressView = (EditText) view.findViewById(R.id.change_adress);



        Retrofit retrofit=new  Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitService retrofitService=retrofit.create( RetrofitService.class);
        Call<MeResult> call=retrofitService.getUserById(getUserId());
        call.enqueue(new Callback<MeResult>() {
            @Override
            public void onResponse(Call<MeResult> call, Response<MeResult> response) {
                userNameView.setText(response.body().getUsername());
                passwordView.setText(response.body(). getUserpass());
                addressView.setText(response.body().getAddress());
              phoneView.setText(response.body().getMobilenum());

            }

            @Override
            public void onFailure(Call<MeResult> call, Throwable t) {

            }
        });
        changeTrue.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Const.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RetrofitService retrofitService = retrofit.create(RetrofitService.class);
                String username = userNameView.getText().toString();
                String userpass = passwordView.getText().toString();
                String mobilenum = phoneView.getText().toString();
                String address = addressView.getText().toString();


                Call<ChangeMeResult> call = retrofitService.updateUserById(getUserId(), username, String.valueOf(userpass), mobilenum, address);
                call.enqueue(new Callback<ChangeMeResult>() {
                    @Override
                    public void onResponse(Call<ChangeMeResult> call, Response<ChangeMeResult> response) {
                        if (response.body().getSuccess().equals("0")) {

                            Toast.makeText(getActivity(), "修改失败", Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_SHORT).show();

    }
                    }
                        @Override
                        public void onFailure(Call<ChangeMeResult> call, Throwable t) {

                        }
                });
            }
        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

}
